package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.BookLabel;
import com.dingreading.cloud.admin.service.BookLabelService;
import com.dingreading.cloud.common.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/bookLabel")
@Api(tags = "总部-书籍分类管理相关")
public class BookLabelController {

    @Resource
    private BookLabelService bookLabelService;

    @ApiOperation(value = "获取所有")
    @PostMapping(value = "/getAll", produces = "application/json; charset=UTF-8")
    public R<Object> getAll(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName
    ) {
        List<BookLabel> bookLabelList = bookLabelService.getAllOrderByOrderNumAsc();
        return R.ok(bookLabelList);
    }

    @ApiOperation(value = "新增(他人勿用)")
    @PostMapping(value = "/editBookLabel", produces = "application/json; charset=UTF-8")
    public R<Object> editBookLabel(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "图片") @RequestParam(value = "img") String img,
            @ApiParam(value = "名称") @RequestParam(value = "labelName") String labelName,
            @ApiParam(value = "序号") @RequestParam(value = "orderNum") Integer orderNum
    ) {
        if (id == null || id <= 0) {
            BookLabel bookLabel = new BookLabel();
            bookLabel.setLabelName(labelName);
            bookLabel.setOrderNum(orderNum);
            bookLabel.setImg(img);
            bookLabel.setEnabled(1);
            bookLabelService.saveOrUpdate(bookLabel);
        }
        return R.ok();
    }

}
